import Taro from '@tarojs/taro';
import { loginBySkey } from '@/api/application';
import { AtModal, AtModalHeader, AtModalContent, AtModalAction, AtButton } from 'taro-ui';
import "taro-ui/dist/style/components/icon.scss";
import "taro-ui/dist/style/components/modal.scss";
import css from './index.module.less';

export default function Login(props) {
    const { modalOpen, setModalOpen, setName, setAvatarUrl, setIsLogin } = props;

    const host = 'http://localhost:8082';
    const loginWeApp = (datas) => {
        Taro.request({
            url: `${host}/sysWxUser/login`,
            data: datas,
            method: 'POST',
            header: { 'content-type': 'application/json' },
            success: function (r) {
                Taro.setStorage({ key: "userInfo", data: r.data.data.userInfo });
                Taro.setStorage({ key: "skey", data: r.data.data.skey });
            },
        });
    };

    // 弹窗取消
    const modalCancel = () => {
        setModalOpen(false);
    };

    // 弹窗确定
    const modalConfirm = () => {
        Taro.getUserProfile({
            desc: '用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                const userInfo = res.userInfo;
                setName(userInfo.nickName);
                setAvatarUrl(userInfo.avatarUrl);
                setIsLogin(true);
                Taro.getStorage({
                    key: 'skey',
                    success: function (a) {
                        loginBySkey(a.data).then(r => {
                            if (r.code === 200) {
                                Taro.setStorage({ key: "userInfo", data: r.data });
                            } else {
                                Taro.login({
                                    success: function (login) {
                                        if (login.code) {
                                            const newData = {
                                                code: login.code,//临时登录凭证
                                                rawData: res.rawData,//用户非敏感信息
                                                signature: res.signature,//签名
                                                encrypteData: res.encryptedData,//用户敏感信息
                                                iv: res.iv//解密算法的向量
                                            };
                                            loginWeApp(newData);
                                        }
                                    },
                                });
                            }
                        });
                    },
                    fail: function () {
                        Taro.login({
                            success: function (login) {
                                if (login.code) {
                                    const newData = {
                                        code: login.code,//临时登录凭证
                                        rawData: res.rawData,//用户非敏感信息
                                        signature: res.signature,//签名
                                        encrypteData: res.encryptedData,//用户敏感信息
                                        iv: res.iv//解密算法的向量
                                    };
                                    loginWeApp(newData);
                                }
                            },
                        });
                    }
                });

            },
        });
        setModalOpen(false);
    };

    return (
        <AtModal className={css.modal} isOpened={modalOpen}>
            <AtModalHeader className={css.modalHeader}>温馨提示</AtModalHeader>
            <AtModalContent className={css.modalContent}>
                请先登录/注册
            </AtModalContent>
            <AtModalAction className={css.modalAction}>
                <AtButton className={css.cancelButton} circle onClick={modalCancel}>取消</AtButton>
                <AtButton className={css.confirmButton} circle onClick={modalConfirm}>确定</AtButton>
            </AtModalAction>
        </AtModal>
    );
}